package com.site.wisdom_construction.controller.statisticsModule;

import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.TwoDataResult;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerStatisticsService;
import com.site.wisdom_construction.service.statisticsModule.StatisticsSafetyService;
import com.site.wisdom_construction.statisticsResult.ListCount;
import com.site.wisdom_construction.statisticsResult.SafetyByHt;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/** @Author daiyifei @Date 2022/2/12 @Description */
@Api(tags = "安全模块统计数据接口")
@RestController
@RequestMapping("StatisticsSafety")
@CrossOrigin
public class StatisticsSafetyController {
  @Autowired private SafetyDangerStatisticsService safetyDangerStatisticsService;

  @Autowired private StatisticsSafetyService statisticsSafetyService;

  @Autowired private SafetyDangerRecordService safetyDangerRecordService;

  @ApiOperation(value = "安全隐患总数据", notes = "测试数据:")
  @PostMapping("/SafetyByHt")
  @NoToken
  public OneDataResult SafetyByHt(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    SafetyByHt safetyByHt = new SafetyByHt();
    // 安全隐患总数
    safetyByHt.setCountSafetyAll(statisticsSafetyService.countSafetyAll(map));
    // 待整改安全隐患数
    safetyByHt.setCountSafetyAllByStatusDZ(statisticsSafetyService.countSafetyAllByStatusDZ(map));
    // 整改待验安全隐患数
    safetyByHt.setCountSafetyAllByStatusDY(statisticsSafetyService.countSafetyAllByStatusDY(map));
    // 已整改安全隐患数
    safetyByHt.setCountSafetyAllByStatusYZ(statisticsSafetyService.countSafetyAllByStatusYZ(map));
    // 超期未整改安全隐患数
    safetyByHt.setCountSafetyAllByStatusCQ(statisticsSafetyService.countSafetyAllByStatusCQ(map));

    Double CountSafetyAll = Double.valueOf(statisticsSafetyService.countSafetyAll(map));
    Double CountSafetyAllByStatusY =
        Double.valueOf(statisticsSafetyService.countSafetyAllByStatusYZ(map));
    Double d = (CountSafetyAllByStatusY / CountSafetyAll) * 100;

    String workerTrain = String.valueOf(d);
    // 整改率
    if ("NaN".equals(workerTrain) || "0.0".equals(workerTrain)) {
      safetyByHt.setRectificationRate("0");
    } else if ("100.0".equals(workerTrain)) {
      safetyByHt.setRectificationRate("100");
    } else {
      if (workerTrain.length() == 3) {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 3));
      } else if (workerTrain.length() == 4) {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 4));
      } else {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 5));
      }
    }
    oneDataResult.setData(safetyByHt);
    return oneDataResult;
  }

  @ApiOperation(value = "根据时间统计数据", notes = "测试数据:")
  @PostMapping("/countByDiscoverTime")
  @NoToken
  public OneDataResult countByDiscoverTime(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByDiscoverTime(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据安全隐患类型分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySafetyTypeName")
  @NoToken
  public OneDataResult countBySafetyTypeName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countBySafetyTypeName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据劳务队伍分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySubcontractorsName")
  @NoToken
  public OneDataResult countBySubcontractorsName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countBySubcontractorsName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据巡检点分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySafetyInspectionName")
  @NoToken
  public OneDataResult countBySafetyInspectionName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countBySafetyInspectionName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据班组分类统计数据", notes = "测试数据:")
  @PostMapping("/countByTeamName")
  @NoToken
  public OneDataResult countByTeamName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByTeamName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据负责人分类统计数据", notes = "测试数据:")
  @PostMapping("/countByResponsiblePerson")
  @NoToken
  public OneDataResult countByResponsiblePerson(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByResponsiblePerson(map));
    return oneDataResult;
  }

  @ApiOperation(value = "当月安全隐患情况分析", notes = "测试数据:")
  @PostMapping("/selectSafetyDangerStatistics")
  @NoToken
  public OneDataResult selectSafetyDangerStatistics(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(safetyDangerStatisticsService.selectSafetyDangerStatistics(map));
    return oneDataResult;
  }

  @ApiOperation(value = "新安全统计" , notes = "测试数据:")
  @PostMapping("/countSafetyDangerStatisticsNew")
  public OneDataResult countRiskShowNew(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    List<ListCount> list = new LinkedList<>();
    ListCount listCountY = new ListCount();
    listCountY.setTypeName("已整改");
    listCountY.setAmount(statisticsSafetyService.countSafetyAllByStatusYZ(map));
    list.add(listCountY);

    ListCount listCountW = new ListCount();
    listCountW.setTypeName("待整改");
    listCountW.setAmount(statisticsSafetyService.countSafetyAllByStatusDZ(map));
    list.add(listCountW);

    ListCount listCountC = new ListCount();
    listCountC.setTypeName("超期未整改");
    listCountC.setAmount(statisticsSafetyService.countSafetyAllByStatusCQ(map));
    list.add(listCountC);
    oneDataResult.setData(list);
    return oneDataResult;
  }

  @ApiOperation(value = "统计安全巡检点总数", notes = "测试数据:")
  @PostMapping("/countAll")
  @NoToken
  public OneDataResult countAll(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countAll(map));
    return oneDataResult;
  }

  @ApiOperation(value = "安全隐患趋势", notes = "测试数据:")
  @PostMapping("/countSafetyDangerRecord")
  @NoToken
  public TwoDataResult countSafetyDangerRecord(@RequestBody Map map) {
    TwoDataResult twoDataResult = new TwoDataResult();
    twoDataResult.setDataOne(safetyDangerRecordService.countSafetyDangerRecord(map));
    String time = (String) map.get("discoverTime");
    map.put("discoverTime", TimeUtils.getDateByMonth(time,-1));
    twoDataResult.setDataTwo(safetyDangerRecordService.countSafetyDangerRecord(map));
    return twoDataResult;
  }
}
